

import hashlib
import os
import json

import requests
import time
from datetime import datetime

serverurl = "https://test.whfciot.com"
api = "/open/api/v1/auth/token"
appKey = "sDHYqAJjSF4e1nnzjan6Ua7s5eCaiPw7"
appSecret =  "iIT0r1148dNmjAzxExBN2JfzjHHQuy8j"

def getTimestamp():
    return str(int(round(time.time() * 1000)))


d = [
    ("appKey", appKey),
    ("randomStr", "0000111100001111"),
    ("timestamp", getTimestamp()),
]

stringA = ""
for key, value in d:
    stringA += key + "=" + value + "&"

stringA += "appSecret=" + appSecret

md5 = (hashlib.md5(stringA.encode('utf-8')).hexdigest()).upper()
print(md5)

dd = {}
for key, value in d:
    dd[key] = value
dd["sign"] = md5

r = requests.post(serverurl + api, json = dd)
print(r, r.text)
ans = eval(r.text)

accessToken = ans["data"]["accessToken"]
print(accessToken)


data = {
    "algDeviceId": "1",
    "deviceName": "fcwl",
    "type": "10",
    "typeName": "tired",
    "time": str(datetime.now()),
}

file = {
    "img": open("./deviceId/1/1020201224113924.jpg", 'rb')
}
papi = "/open/api/v1/fim/alg/result/upload?accessToken={0}".format(accessToken)
r = requests.post(serverurl + papi, data= data, files = file)
print(r, r.text)
